package com.ruoyi.project.pandian.pandian.service;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.project.bas.basJobId.service.IBasJobIdService;
import com.ruoyi.project.out.outStock.domain.OutStockout;
import com.ruoyi.project.out.outStock.domain.OutStockoutVo;
import com.ruoyi.project.out.outStock.mapper.OutStockoutMapper;
import com.ruoyi.project.out.outStockDetail.domain.OutStockoutDetail;
import com.ruoyi.project.out.outStockDetail.mapper.OutStockoutDetailMapper;
import com.ruoyi.project.pandian.pandian.mapper.PDMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class PDServiceImpl implements PDService{
    @Autowired
    IBasJobIdService basJobIdService;
    @Autowired
    OutStockoutMapper outStockoutMapper;
    @Autowired
    OutStockoutDetailMapper outStockoutDetailMapper;
    @Autowired
    PDMapper pdMapper;

    @Override
    public String addSavePDstock(OutStockoutVo outStockoutVo) {

        OutStockout outStockout = outStockoutVo.getOutStockout();
        List<OutStockoutDetail> outStockoutDetailList = outStockoutVo.getOutStockoutDetailList();
        //获取出库单号
        String stockoutId = basJobIdService.getJobId("PD");
        outStockout.setStockoutGuid(UUID.randomUUID().toString());
        outStockout.setStockoutId(stockoutId);
        outStockout.setStatus("1");
//        outStockout.setWarehouseId("01");
//        String customerName = basCustomerMapper.selectCustomerByNo(outStockout.getCustomerNo());
//        outStockout.setClassNowId(customerName);
        outStockout.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        //获取当前操作员
        SysUser user = (SysUser) ServletUtils.getSubject().getPrincipal();
        String userName = null;
        userName = user.getUserName();
        outStockout.setCreateManId(userName);
        //wms自建单
        outStockout.setStockoutSource("0");
        outStockoutMapper.insertOutStockout(outStockout);
        //
        for (int i = 0; i < outStockoutDetailList.size(); i++) {
            OutStockoutDetail outStockoutDetail = outStockoutDetailList.get(i);
            outStockoutDetail.setStockoutDetailId(stockoutId + "-" + (i + 1));
            outStockoutDetail.setStockoutGuid(outStockout.getStockoutGuid());
            outStockoutDetail.setStockoutId(stockoutId);
            outStockoutDetail.setAllotQuantity(BigDecimal.ZERO);
            outStockoutDetail.setAssignNum(BigDecimal.ZERO);
            outStockoutDetailMapper.insertOutStockoutDetail(outStockoutDetail);
        }
        return stockoutId;
    }

    @Override
    public Map<String, Object> autoAllocation(String[] stockoutIds, String outType,String exitId) {
        StringBuilder builder = new StringBuilder();
        Map<String, Object> resultMap = new HashMap<String, Object>();
        for (int i = 0; i < stockoutIds.length; i++) {

            OutStockoutDetail outStockoutDetail = new OutStockoutDetail();
            outStockoutDetail.setStockoutId(stockoutIds[i]);

            // 修改单据拣选台号
            if (stockoutIds[i] != null && !stockoutIds[i].equals("") && exitId != null && !exitId.equals("")){
                outStockoutMapper.updateExitById(stockoutIds[i],exitId);
            }


            List<OutStockoutDetail> outStockoutDetailList = outStockoutDetailMapper.selectOutStockoutDetailList(outStockoutDetail);
            Map<String, String> map = new HashMap<>();
            for (int j = 0; j < outStockoutDetailList.size(); j++) {
                String msg = "";
                int i1 = outStockoutDetailList.get(j).getAllotQuantity().compareTo(outStockoutDetailList.get(j).getQuantity());
                if (i1 >-1){
                    msg = "单据数量已全部分配！";
                    builder.append(outStockoutDetailList.get(j).getStockoutDetailId() + ":" + msg);
                    resultMap.put("errorInfo", builder.toString());
                    return resultMap;
                }
                if (outStockoutDetailList.get(j).getFid() != null) {
                    map.put("v_stockout_detail_id", outStockoutDetailList.get(j).getStockoutDetailId());
                    map.put("v_out_id", exitId);
                    pdMapper.autoAllocationByTray(map);
                    String result = map.get("v_result");

                    if ("1".equals(result)) {
                        msg = "单据不存在,请核对！";
                    } else if ("2".equals(result)) {
                        msg = "托盘库存不存在,请核对！";
                    } else if ("3".equals(result)) {
                        msg = "托盘已有作业,请核对！";
                    } else if ("9".equals(result)) {
                        msg = "处理异常,请重试！";
                    } else if ("10".equals(result)) {
                        msg = "分配成功！";
                    }

                    builder.append(outStockoutDetailList.get(j).getStockoutDetailId() + ":" + msg);
                } else {
                    map.put("v_stockout_detail_id", outStockoutDetailList.get(j).getStockoutDetailId());
                    map.put("v_out_id", exitId);
                    pdMapper.autoAllocationByPro(map);
                    String result = map.get("v_result");
                    if ("1".equals(result)) {
                        msg = "单据不存在,请核对！";
                    } else if ("2".equals(result)) {
                        msg = "托盘库存不存在,请核对！";
                    } else if ("3".equals(result)) {
                        msg = "该出口已有生产计划，请稍后分配！";
                    } else if ("9".equals(result)) {
                        msg = "处理异常,请重试！";
                    } else if ("10".equals(result)) {
                        msg = "分配成功！";
                    }
                    builder.append(outStockoutDetailList.get(j).getStockoutDetailId() + ":" + msg);
                }
                }
            }
            resultMap.put("errorInfo", builder.toString());
            return resultMap;
        }

    @Override
    public String assignCargoPd(String trayCode, String exitId) {
        String msg="";
        HashMap<String, String> map = new HashMap<>();
        map.put("v_traycode",trayCode);
        map.put("v_exit_id",exitId);
        pdMapper.assignCargoPd(map);
        String result=map.get("v_result");
        if("1".equals(result)){
            msg="该托盘无库存";
        }else if("2".equals(result)){
            msg="该托盘已有作业";
        }else if("3".equals(result)){
            msg="该出口已有生产计划，请稍后分配！";
        }else if("9".equals(result)){
            msg="存储过程异常";
        }else{
            msg="操作成功";
        }
        return msg;
    }
}
